/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * frmPartidoPolitico.java
 *
 * Created on 21/05/2011, 05:12:18 PM
 */

package verins.GUI;

import java.text.SimpleDateFormat;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JDialog;
import verins.BO.PartidopoliticoBO;
import verins.BO.TipoPartidoPoliticoBO;
import verins.beans.Partidopolitico;
import verins.beans.Tipopartidopolitico;



import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import verins.Utils.LimitInput;
/**
 *
 * @author yisse
 */
public class frmPartidoPolitico extends javax.swing.JDialog {
     private Partidopolitico partidoPolitico =null;
      private JDialog caller=null;
      private String correo=null;

    /** Creates new form frmPartidoPolitico */
    public frmPartidoPolitico(Partidopolitico partido, JDialog ventana) {
        super(ventana,true);
        caller=ventana;
        partidoPolitico=partido;
        
        initComponents();
         verins.Utils.Comunes.centrarVentana(this);
       
        setearValidadores();
         llenarCombos();
        
       if (partido!=null)
       MostrarDatos();


    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        lblNombrePartido = new javax.swing.JLabel();
        txtNombrePartido = new javax.swing.JTextField();
        lblRuc = new javax.swing.JLabel();
        txtRuc = new javax.swing.JTextField();
        lblDireccion = new javax.swing.JLabel();
        txtDireccion = new javax.swing.JTextField();
        lblTipo = new javax.swing.JLabel();
        cmbTipo = new javax.swing.JComboBox();
        lblTelefonos = new javax.swing.JLabel();
        txtTelefonos = new javax.swing.JTextField();
        txtEmail = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        cmbEstado = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        lblTituloCodigo = new javax.swing.JLabel();
        lblTituloFechaR = new javax.swing.JLabel();
        jLayeredPane1 = new javax.swing.JLayeredPane();
        lblCodigo = new javax.swing.JLabel();
        lblFecha = new javax.swing.JLabel();
        btnGuardar = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Partido Político");
        setResizable(false);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Datos del Partido Político"));

        lblNombrePartido.setText("Nombre del Partido");

        lblRuc.setText("RUC");

        lblDireccion.setText("Dirección");

        lblTipo.setText("Tipo");

        lblTelefonos.setText("Teléfonos");

        txtTelefonos.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtTelefonosActionPerformed(evt);
            }
        });

        jLabel1.setText("Email");

        cmbEstado.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Activo", "Inactivo" }));

        jLabel2.setText("Estado");

        lblTituloCodigo.setText("Código :");

        lblTituloFechaR.setText("Fecha de Registro");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(lblNombrePartido, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(lblRuc, javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(lblDireccion, javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(lblTipo, javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(lblTelefonos, javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING))
                    .addComponent(lblTituloFechaR, javax.swing.GroupLayout.PREFERRED_SIZE, 153, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(59, 59, 59)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(cmbTipo, javax.swing.GroupLayout.PREFERRED_SIZE, 220, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(txtEmail, javax.swing.GroupLayout.PREFERRED_SIZE, 314, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(txtTelefonos, javax.swing.GroupLayout.PREFERRED_SIZE, 206, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(txtRuc, javax.swing.GroupLayout.PREFERRED_SIZE, 194, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addComponent(txtDireccion, javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(txtNombrePartido, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 327, Short.MAX_VALUE)))
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(jPanel1Layout.createSequentialGroup()
                                    .addGap(74, 74, 74)
                                    .addComponent(jLabel2)
                                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(jPanel1Layout.createSequentialGroup()
                                            .addGap(326, 326, 326)
                                            .addComponent(lblCodigo))
                                        .addGroup(jPanel1Layout.createSequentialGroup()
                                            .addGap(18, 18, 18)
                                            .addComponent(cmbEstado, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE))))
                                .addGroup(jPanel1Layout.createSequentialGroup()
                                    .addGap(122, 122, 122)
                                    .addComponent(lblTituloCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                    .addComponent(lblFecha, javax.swing.GroupLayout.PREFERRED_SIZE, 213, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(39, 39, 39))
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addGap(0, 469, Short.MAX_VALUE)
                    .addComponent(jLayeredPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(0, 469, Short.MAX_VALUE)))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblNombrePartido)
                    .addComponent(txtNombrePartido, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblCodigo)
                    .addComponent(lblTituloCodigo))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblRuc)
                            .addComponent(txtRuc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(28, 28, 28)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblDireccion)
                            .addComponent(txtDireccion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(36, 36, 36)
                                .addComponent(lblTipo))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(18, 18, 18)
                                .addComponent(cmbTipo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(26, 26, 26)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblTelefonos)
                            .addComponent(txtTelefonos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(26, 26, 26)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel1))
                        .addGap(29, 29, 29)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblTituloFechaR, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblFecha)))
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel2)
                        .addComponent(cmbEstado, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(40, Short.MAX_VALUE))
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addGap(0, 148, Short.MAX_VALUE)
                    .addComponent(jLayeredPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(0, 148, Short.MAX_VALUE)))
        );

        btnGuardar.setText("Guardar");
        btnGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGuardarActionPerformed(evt);
            }
        });

        btnCancelar.setText("Cancelar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnGuardar)
                        .addGap(18, 18, 18)
                        .addComponent(btnCancelar))
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 841, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnCancelar)
                    .addComponent(btnGuardar))
                .addContainerGap(17, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents


    private void llenarCombos(){
        List lista= TipoPartidoPoliticoBO.findAll();
        verins.Utils.Comunes.llenarCombo(cmbTipo,lista);
        
    }


    private boolean validarCampos(){
    boolean bOk=true;
        if (txtNombrePartido.getText().trim().length()==0){
            verins.Utils.Mensajes.alerta("Debe ingresar nombre de partido");
            return false;
        }
        if (txtRuc.getText().trim().length()==0){
            verins.Utils.Mensajes.alerta("Debe ingresar RUC de partido");
            return false;
        }
        
         if (!isEmail( txtEmail.getText())){
            verins.Utils.Mensajes.alerta("Debe ingresar Email Valido");
            return false;
        }
        
    return bOk;
    }



     private void setearValidadores(){
        txtRuc.setDocument(new verins.Utils.LimitInput(13));
        txtRuc.addKeyListener(verins.Utils.Verificador.verificaSoloNumeros());
        txtNombrePartido.setDocument(new verins.Utils.LimitInput(25));
        txtNombrePartido.addKeyListener(verins.Utils.Verificador.verificaSoloCaracteresValidos());
        txtDireccion.setDocument(new verins.Utils.LimitInput(20));
        txtTelefonos.addKeyListener(verins.Utils.Verificador.verificaSoloNumeros());  
        txtTelefonos.setDocument(new LimitInput(20));
       // correo=txtEmail.getText();
        
        //txtEmail.setDocument(new verins.Utils.ValidarEmail(correo));
        
    }




    private void txtTelefonosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtTelefonosActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtTelefonosActionPerformed

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        

         try{
            if (((JButton)evt.getSource()).getText().equalsIgnoreCase("cancelar")
                  ){
                if (verins.Utils.Mensajes.cancelar(evt,this)){
                    this.dispose();
                }
            }else{//salir
                this.dispose();
            }
        }catch(Exception ex){
            dispose();
        }

    }//GEN-LAST:event_btnCancelarActionPerformed

         private void btnGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGuardarActionPerformed
             // TODO add your handling code here:

             if (((JButton) evt.getSource()).getText().equalsIgnoreCase("guardar")) {
                 if (validarCampos()){
                     if (verins.Utils.Mensajes.guardar(evt, this)) {
                         partidoPolitico = obtenerDatos();
                         if (verins.Utils.Mensajes.exito(PartidopoliticoBO.save(partidoPolitico))) {
                             this.dispose();
                         }
                     }
                 }
             } else {//
                 bloquearCamposEditar();
                 setearBotonGuardarEditar("Guardar");
                 setearBotonSalirCancelar("Cancelar");
             }


         }//GEN-LAST:event_btnGuardarActionPerformed


         public void bloquearCamposEditar(){

                txtNombrePartido.setEditable(false);
      txtRuc.setEditable(false);
      txtDireccion.setEditable(true);
      cmbTipo.setEnabled(true);
      txtTelefonos.setEditable(true);
      txtEmail.setEditable(true);
      cmbEstado.setEnabled(true);



         }






       private Partidopolitico obtenerDatos(){
        Partidopolitico partido=null;
        //valida
        if (partidoPolitico==null){
            partido=new Partidopolitico();
            partido.setIdpartidopolitico(0);
            partido.setNombre(txtNombrePartido.getText());
            partido.setRuc(txtRuc.getText());
            partido.setDireccion(txtDireccion.getText());
            partido.setIdtipopartidopolitico((Tipopartidopolitico)cmbTipo.getSelectedItem());
            partido.setTelefonos(txtTelefonos.getText());
            partido.setEmail(txtEmail.getText());
            partido.setFecharegistro(new java.util.Date());
            partido.setEstado(cmbEstado.getSelectedItem().toString());
            partidoPolitico=partido;

            return partido;
        }else{
            partidoPolitico.setNombre(txtNombrePartido.getText());
            partidoPolitico.setRuc(txtRuc.getText());
            partidoPolitico.setDireccion(txtDireccion.getText());
            partidoPolitico.setIdtipopartidopolitico((Tipopartidopolitico)cmbTipo.getSelectedItem());
            partidoPolitico.setTelefonos(txtTelefonos.getText());
            partidoPolitico.setEmail(txtEmail.getText());
            partidoPolitico.setFecharegistro(new java.util.Date());
            partidoPolitico.setEstado(cmbEstado.getSelectedItem().toString());

            return partidoPolitico;
        
        }
    }


     private void MostrarDatos()
    {
        
        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
        txtNombrePartido.setText(partidoPolitico.getNombre());
        txtRuc.setText(partidoPolitico.getRuc());
        txtDireccion.setText(partidoPolitico.getDireccion());
        cmbTipo.setSelectedItem(partidoPolitico.getIdtipopartidopolitico());
        txtTelefonos.setText(partidoPolitico.getTelefonos());
        txtEmail.setText(partidoPolitico.getEmail());
        cmbEstado.setSelectedItem(partidoPolitico.getEstado());
        lblFecha.setText(sdf.format(partidoPolitico.getFecharegistro()));              
        lblTituloCodigo.setText(lblTituloCodigo.getText()+ " " + partidoPolitico.getIdpartidopolitico()) ;

    }



   public void bloquearCamposMostrar()
    {
      txtNombrePartido.setEditable(false);
      txtRuc.setEditable(false);
      txtDireccion.setEditable(false);
      cmbTipo.setEnabled(false);
      txtTelefonos.setEditable(false);
      txtEmail.setEditable(false);
      cmbEstado.setEnabled(false);

     }



    public void setearBotonSalirCancelar(String texto){
        btnCancelar.setText(texto);
    }

     public void setearBotonGuardarEditar(String texto){
        btnGuardar.setText(texto);
    }


     public void mostrarBotonGuardar(boolean b){
         btnGuardar.setVisible(b);
    }

   private void HabilitarCampos()
    {
      txtNombrePartido.setEditable(true);
      txtRuc.setEditable(true);
      txtDireccion.setEditable(true);
      txtTelefonos.setEditable(true);
      txtEmail.setEditable(true);
      
     }

  //metodo para validar correo electronio
    public boolean isEmail(String correo) {
        Pattern pat = null;
        Matcher mat = null;
        pat = Pattern.compile("^([0-9a-zA-Z]([_.w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-w]*[0-9a-zA-Z].)+([a-zA-Z]{2,9}.)+[a-zA-Z]{2,3})$");
        mat = pat.matcher(correo);
        if (mat.find()) {
            System.out.println("[" + mat.group() + "]");
            return true;
        }else{
            return false;
        }
    }



    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new frmPartidoPolitico(null,null).setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnGuardar;
    private javax.swing.JComboBox cmbEstado;
    private javax.swing.JComboBox cmbTipo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLayeredPane jLayeredPane1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel lblCodigo;
    private javax.swing.JLabel lblDireccion;
    private javax.swing.JLabel lblFecha;
    private javax.swing.JLabel lblNombrePartido;
    private javax.swing.JLabel lblRuc;
    private javax.swing.JLabel lblTelefonos;
    private javax.swing.JLabel lblTipo;
    private javax.swing.JLabel lblTituloCodigo;
    private javax.swing.JLabel lblTituloFechaR;
    private javax.swing.JTextField txtDireccion;
    private javax.swing.JTextField txtEmail;
    private javax.swing.JTextField txtNombrePartido;
    private javax.swing.JTextField txtRuc;
    private javax.swing.JTextField txtTelefonos;
    // End of variables declaration//GEN-END:variables

}
